package com.example.drug.controller;


import com.example.drug.domain.Drug;
import com.example.drug.service.DrugService;
import com.example.drug.util.AjaxResult;
import com.example.drug.util.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;





/**
 * 药品Controller
 *
 * @author [你的名字]
 * @date [具体日期]
 */
@RestController
@RequestMapping("/api/drug")
@Api(tags = "药品管理609余函池")
public class DrugController {

    @Autowired
    private DrugService drugService;

    /**
     * 获取全部药品列表
     */
    @ApiOperation("获得全部药品列表609余函池")
    @GetMapping("/druglist")
    public AjaxResult getAllDrugsList() {
        return AjaxResult.success(drugService.selectAllDrugs());
    }

    /**
     * 查询药品列表
     */
    @ApiOperation("查询药品列表609余函池")
    @ApiImplicitParam(name = "name", value = "药品名称",
            dataType = "string", dataTypeClass = String.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name", required = false) String drugName) {
        List<Drug> list = drugService.selectDrugByParam(drugName);
        return AjaxResult.success(list);
    }

    /**
     * 获取药品详细信息
     */
    @ApiOperation("获取药品详细信息609余函池")
    @ApiImplicitParam(name = "id", value = "药品ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Drug drug = drugService.selectDrugById(id);
        if (drug == null || drug.getDrugId() == null) {
            throw new BizException(404, "药品 " + id + " 不存在");
        }
        return AjaxResult.success(drug);
    }

    /**
     * 新增药品
     */
    @ApiOperation("新增药品609余函池")
    @PostMapping
    public AjaxResult add(@RequestBody Drug drug) {
        return AjaxResult.success(drugService.insertDrug(drug));
    }

    /**
     * 修改药品
     */
    @ApiOperation("修改药品609余函池")
    @PutMapping
    public AjaxResult edit(@RequestBody Drug drug) {
        return AjaxResult.success(drugService.updateDrug(drug));
    }

    /**
     * 删除药品
     */
    @ApiOperation("删除药品609余函池")
    @ApiImplicitParam(name = "id", value = "药品ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(drugService.deleteDrugById(id));
    }
}